package com.example.newapp.service.impl;
import com.example.newapp.bean.Account;
import com.example.newapp.dao.AccountMapper;
import com.example.newapp.service.IAccountService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AccountServiceImpl implements IAccountService {
    @Resource
    private AccountMapper accountMapper;

    @Override
    public void save(Account account) {
        Account dbaccount = accountMapper.selectByAccount(account.getAccount());
        if(dbaccount != null){
            throw new RuntimeException("账户名不可用");
        }
        account.setStatus("状态正常");
        account.setLoginTime(new Date().getTime());
        accountMapper.insert(account);
    }

    @Override
    public List<Account> findAll() {
        return accountMapper.selectAll();
    }

    @Override
    public Account findById(long id) {
        return accountMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(long id) {
        Account account = this.findById(id);
        if(account == null){
            throw new RuntimeException("账号不存在");
        }
        accountMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateById(Account account) {
        Account acnt =this.findById(account.getId());
        if(acnt == null){
            accountMapper.insert(account);
        }else {
            accountMapper.updateByPrimaryKey(account);
        }
    }

}
